package com.wzy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.mapper.VisitorMapper;
import com.wzy.pojo.Visitor;
import com.wzy.service.VisitorService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static java.rmi.server.LogStream.log;

@Slf4j
@Service
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper, Visitor> implements VisitorService {

    @Autowired
    VisitorMapper visitorMapper;

    @Override
    public List<Visitor> getAllVisitor() {
        List<Visitor> list = visitorMapper.selectAllVisitor();
        return list;
    }

    @Override
    public Integer deleteById(Integer id) {
        Integer i = visitorMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer addOne(String ip, String city, String flag, String time) {
        List<Visitor> list = visitorMapper.selectAllVisitor();
        boolean f = false;
        int id = 0;
        for (Visitor item : list) {
            if (item.getVisitorIp().equals(ip)) {
                System.out.println(item.getVisitorIp().equals(ip));
                f = true;
                id = item.getId();
                break;
            }
        }
        if (f) {
            log("数据库中已有值,重复不能插入");
            SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = s1.format(new Date());
            visitorMapper.updateById(format,id);//没有插入，但更新了最后一次登录时间
            return -1;
        } else {
            Integer i = visitorMapper.insertOne(ip, city, flag, time);
            return i;
        }
    }


}
